package com.repair.web.handle;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.repair.base.exception.BizException;
import com.repair.base.exception.ErrorCode;
import com.repair.base.exception.SystemException;
import com.repair.base.result.JsonResult;
import com.repair.base.utils.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author chunyu
 * @since 2024/9/14,11:10
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {


    /**
     * 非法参数异常
     * Validate
     *
     * @param ex
     * @return
     */
    //@ExceptionHandler
    public JsonResult<String> doHandleIllegalArgumentException(IllegalArgumentException ex) {
        String message = ex.getMessage();
        log.error("参数无效 message--{}", message);
        return JsonResult.error(ResponseCode.ILLEGAL_ARGUMENT.getCode(), message);
    }

    @ExceptionHandler(BizException.class)
    public JsonResult<String> doHandleBizException(BizException ex) {
        ErrorCode errCode = ex.getErrorCode();
        String message = errCode.getMessage();
        return JsonResult.error(errCode.getCode(), message);
    }

    @ExceptionHandler(SystemException.class)
    public JsonResult<String> doHandleSystemException(SystemException ex) {
        ErrorCode errCode = ex.getErrorCode();
        String message = errCode.getMessage();
        return JsonResult.error(errCode.getCode(), message);
    }

    //@ExceptionHandler
    public JsonResult<String> doHandleRuntimeException(RuntimeException ex) {
        String message = ex.getMessage();
        return JsonResult.error(500, message);
    }

    @ExceptionHandler(NotLoginException.class)
    public JsonResult<String> doHandleNotLoginException(NotLoginException ex) {
        return JsonResult.error(ResponseCode.NOT_LOGIN.getCode(), ex.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public JsonResult<String> doHandleNotPermissionException(NotPermissionException ex) {
        return JsonResult.error(ResponseCode.NOT_AUTH.getCode(), ex.getMessage());
    }


}
